package com.jinkaimen.goodcash.ui.activity;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.pullrefresh.PullToRefreshBase;
import com.ht.baselibs.ui.widget.pullrefresh.PullToRefreshListView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.NetUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.BalanceBean;
import com.jinkaimen.goodcash.bean.WithDrawListBean;
import com.jinkaimen.goodcash.bean.WithDrawListItemBean;
import com.jinkaimen.goodcash.dao.AccountDao;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.ui.adapter.WithdrawAdapter;
import com.jinkaimen.goodcash.utils.NumberUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class AccountActivity extends BaseActivity {

    private ListView mListView;
    private TextView mTotolBalanceView;
    private TextView mAwaitBalanceView;
    private TextView mAvailBalanceView;
    private List<WithDrawListItemBean> mDataList;
    private WithdrawAdapter mWithdrawAdapter;
    private PullToRefreshListView mPullToRefreshListView;
    private int mPageNo = 1;
    private boolean isBindQrCode;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("余额");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_account;
    }

    @Override
    protected void initChildView() {
        mTotolBalanceView = (TextView) findViewById(R.id.tv_activity_account_balance);
        mAwaitBalanceView = (TextView) findViewById(R.id.tv_activity_account_await_balance);
        mAvailBalanceView = (TextView) findViewById(R.id.tv_activity_account_avail_balance);


        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.lv_activity_account);
        mPullToRefreshListView.setPullLoadEnabled(false);
        mPullToRefreshListView.setScrollLoadEnabled(true);
        mListView = mPullToRefreshListView.getRefreshableView();
    }

    @Override
    protected void initChildData() {
        mDataList = new ArrayList<>();
        mWithdrawAdapter = new WithdrawAdapter(this, mDataList);
        mListView.setAdapter(mWithdrawAdapter);
    }

    @Override
    protected void setChildViewListener() {
        findViewById(R.id.tv_activity_account_withdraw).setOnClickListener(this);

        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!NetUtils.isNetWorkConnection()) {
                    UIUtils.showToast("请检查您的网络设置");
                    return;
                }
                mPullToRefreshListView.resetHasMore();
                mPageNo = 1;
                refreshList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPageNo++;
                refreshList();
            }
        });
        mPullToRefreshListView.doPullRefreshing(true, 800);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBalanceFromNet();
    }

    public void getBalanceFromNet(){
        ApiManager.getApi().getBalance(
                "HSY001033",
                AccountDao.getInstance().getAccount().accountID
        ).enqueue(new NetCallBack<BalanceBean>() {
            @Override
            protected void onSuccess(Call<BalanceBean> call, Response<BalanceBean> response) {
                BalanceBean balanceBean = response.body();
                if (balanceBean.totalAmount != 0){
                    mTotolBalanceView.setText("￥ "+ NumberUtils.getTwoBit(balanceBean.totalAmount));
                } else {
                    mTotolBalanceView.setText("￥ 0");
                }

                if (balanceBean.dueSettleAmount != 0){
                    String format = String.format(getResources().getString(R.string.await_balance), NumberUtils.getTwoBit(balanceBean.dueSettleAmount));
                    mAwaitBalanceView.setText(format);
                } else {
                    mAwaitBalanceView.setText("待结算金额：0元");
                }

                if (balanceBean.available != 0){
                    String format = String.format(getResources().getString(R.string.avail_balance), NumberUtils.getTwoBit(balanceBean.available));
                    mAvailBalanceView.setText(format);
                } else {
                    mAvailBalanceView.setText("可提现金额：0元");
                }

                isBindQrCode = balanceBean.isBindCode;
            }

        });
    }

    private void refreshList() {
        ApiManager.getApi().withdrawList(
                "HSY001035",
                AccountDao.getInstance().getAccount().accountID,
                mPageNo,
                10
        ).enqueue(new NetCallBack<WithDrawListBean>() {
            @Override
            protected void onSuccess(Call<WithDrawListBean> call, Response<WithDrawListBean> response) {
                if (mPageNo == 1){
                    mDataList.clear();
                }
                List<WithDrawListItemBean> recordList = response.body().records;
                mDataList.addAll(recordList);
                if (mPageNo != 1 && recordList.size() <= 0 ){
                    UIUtils.showToast("没有更多内容");
                }
                mWithdrawAdapter.notifyDataSetChanged();
            }

            @Override
            protected void hiddenProgress() {
                if (mPageNo == 1){
                    mPullToRefreshListView.onPullDownRefreshComplete();
                } else {
                    mPullToRefreshListView.onPullUpRefreshComplete();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_activity_account_withdraw:
                if (isBindQrCode){
                    ActivityUtils.startActivity(this, WithdrawActivity.class);
                } else {
                    UIUtils.showToast("请先绑定商铺二维码");
                }
                break;
        }
    }
}
